﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TS2015.Models;
using Kendo.Mvc.UI;
using Kendo.Mvc.Extensions;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Text;
using System.Data.OleDb;
using System.Data;
using System.ComponentModel.DataAnnotations;
namespace TS2015.Areas.Admin.Controllers
{
    public class SearchController : Controller
    {
        TS2015Context db = new TS2015Context();
        // GET: Admin/Search
        public ActionResult Index()
        {
            return View();
        }
        #region Search Candidate
        //[HttpGet]
        public ActionResult SearchCandidate()
        {
            //Database.SetInitializer<TS2015Context>(null);
            // do du lieu len dropdownlist
            ViewBag.TruongTHPT = db.TbTruongTHPTs.ToList();
            ViewBag.DanToc = db.TbDanTocs.ToList();
            ViewBag.ChuyenNganh = db.TbChuyenNganhs.ToList();
            ViewBag.Truong = db.TbTruongTHPTs.ToList();
            ViewBag.Nganh = db.TbNganhs.ToList();
            //ViewBag.Truong = new SelectList(db.TbTruongTHPTs.ToList(), "IDTruong", "TenTruong");
            ViewBag.KhuVuc = db.TbKhuVucs.ToList();
            ViewBag.XepLoaiHS = db.TbXepLoaiHS.ToList();
            ViewBag.DanToc = db.TbDanTocs.OrderBy(m => m.TenDanToc).ToList();
            ViewBag.DoiTuongUuTien = db.TbDoiTuongUuTiens.ToList();
            //ViewBag.KhoiThi = db.TbKhoiThis.ToList();
            ViewBag.HeDaoTao = new SelectList(db.TbHeDaoTaos.ToList(), "MaHeDaoTao", "TenHeDaoTao");
            ViewBag.Tinh = new SelectList(db.TbTinhs.OrderBy(m => m.TenTinh).ToList(), "MaTinh", "TenTinh");
            ViewBag.KhoiThi = new SelectList(db.TbKhoiThis.ToList(), "MaKhoiThi", "TenKhoiThi");
            //ViewBag.BacDaoTao = new SelectList(db.TbBacDaoTaos.Include(e => e.TbHeDaoTao).Select(e => new BacDaoTao { MaBacDT = e.MaBacDT, TenBacDT = e.TenBacDT + " - " + e.TbHeDaoTao.TenHeDaoTao }).OrderBy(e => e.TenBacDT), "MaBacDT", "TenBacDT");
            //ViewBag.BacDaoTao = new SelectList(db.TbBacDaoTaos.ToList(), "MaBacDT", "TenBacDT");
            ViewBag.DaNop = false;
            ViewBag.ChuaNop = false;
            
            if (Request.IsAjaxRequest())
                return PartialView();
            return View();
        }
        public JsonResult GetListCandidate(string searchID)
        {
            var ListCandidates = from c in db.TbThiSinhs
                                 select c;
            ListCandidates = ListCandidates.Where(m => m.MaTS.Contains(searchID));

            return Json(ListCandidates, JsonRequestBehavior.AllowGet);
        }
        
        [HttpPost]
        [ActionName("SearchCandidate")]
        public ActionResult SearchCandidate2(TbThiSinh item, string HDT, string MaTinh, string MaTinh2, string IDTruong, string BDT, string MaKhoiThi, string MaNganh, bool ChuaNop = false, bool DaNop = false)
        {
            ViewBag.HinhThucDK = from enumValue in Enum.GetValues(typeof(HinhThucDK)).Cast<HinhThucDK>()
                                 select new SelectListItem
                                 {
                                     Text = ((DisplayAttribute)(typeof(HinhThucDK).GetField(enumValue.ToString()).GetCustomAttributes(typeof(DisplayAttribute), false).First())).Name,
                                     Value = ((int)enumValue).ToString(),
                                 };
            IEnumerable<Candidates> CandidateList = (from a in db.TbThiSinhs
                                join b in db.TbDanTocs on a.MaDanToc equals b.MaDanToc
                                join c in db.TbThiSinh_BacDaoTaos on a.MaTS equals c.MaTS
                                join d in db.TbBacDaoTaos on c.MaBacDT equals d.MaBacDT
                                join e in db.TbThiSinh_Truongs on a.MaTS equals e.MaTS
                                join j in db.TbTruongTHPTs on e.IDTruong equals j.IDTruong
                                join x in db.TbTinhs on j.MaTinh equals x.MaTinh
                                join f in db.TbHuyens on a.MaHuyen equals f.MaHuyen
                                join o in db.TbTinhs on f.MaTinh equals o.MaTinh
                                join g in db.TbDoiTuongUuTiens on a.MaDoiTuong equals g.MaDoiTuong
                                join h in db.TbTruongDHs on a.MaTruongDH equals h.MaTruongDH
                                join i in db.TbChuyenNganhs on a.MaChuyenNganh equals i.MaChuyenNganh
                                join k in db.TbNganhs on i.IDNganh equals k.IDNganh
                                join l in db.TbKhoiThis on k.MaKhoiThi equals l.MaKhoiThi
                                where c.DKThem==false
                                select new Candidates
                                {
                                    MaTS = a.MaTS,
                                    HoDem = a.HoDem,
                                    Ten = a.Ten,
                                    FullName = a.HoDem + a.Ten,
                                    SBD = a.SBD,
                                    MaDanToc = a.MaDanToc,
                                    TenDanToc = b.TenDanToc,
                                    GioiTinh = a.GioiTinh,
                                    SoDienThoai = a.SoDienThoai,
                                    Email = a.Email,
                                    NgaySinh = a.NgaySinh,
                                    //IDTruong = e.IDTruong,
                                    MaTinhTruong10 = (from aa in db.TbThiSinh_Truongs
                                                      join bb in db.TbTruongTHPTs on aa.IDTruong equals bb.IDTruong
                                                      join cc in db.TbTinhs on bb.MaTinh equals cc.MaTinh
                                                      where aa.Lop == 10 && aa.MaTS == a.MaTS
                                                      select cc.MaTinh).FirstOrDefault(),
                                    TenTinhTruong10 = (from aa in db.TbThiSinh_Truongs
                                                       join bb in db.TbTruongTHPTs on aa.IDTruong equals bb.IDTruong
                                                       join cc in db.TbTinhs on bb.MaTinh equals cc.MaTinh
                                                       where aa.Lop == 10 && aa.MaTS == a.MaTS
                                                       select cc.TenTinh).FirstOrDefault(),
                                    IDTruong10 = (from aa in db.TbThiSinh_Truongs
                                                  join bb in db.TbTruongTHPTs on aa.IDTruong equals bb.IDTruong
                                                  where aa.Lop == 10 && aa.MaTS == a.MaTS
                                                  select bb.IDTruong).FirstOrDefault(),
                                    TenTruong10 = (from aa in db.TbThiSinh_Truongs
                                                   join bb in db.TbTruongTHPTs on aa.IDTruong equals bb.IDTruong
                                                   where aa.Lop == 10 && aa.MaTS == a.MaTS
                                                   select bb.TenTruong).FirstOrDefault(),
                                    MaTinhTruong11 = (from aa in db.TbThiSinh_Truongs
                                                      join bb in db.TbTruongTHPTs on aa.IDTruong equals bb.IDTruong
                                                      join cc in db.TbTinhs on bb.MaTinh equals cc.MaTinh
                                                      where aa.Lop == 11 && aa.MaTS == a.MaTS
                                                      select cc.MaTinh).FirstOrDefault(),
                                    TenTinhTruong11 = (from aa in db.TbThiSinh_Truongs
                                                       join bb in db.TbTruongTHPTs on aa.IDTruong equals bb.IDTruong
                                                       join cc in db.TbTinhs on bb.MaTinh equals cc.MaTinh
                                                       where aa.Lop == 11 && aa.MaTS == a.MaTS
                                                       select cc.TenTinh).FirstOrDefault(),
                                    IDTruong11 = (from aa in db.TbThiSinh_Truongs
                                                  join bb in db.TbTruongTHPTs on aa.IDTruong equals bb.IDTruong
                                                  where aa.Lop == 11 && aa.MaTS == a.MaTS
                                                  select bb.IDTruong).FirstOrDefault(),
                                    TenTruong11 = (from aa in db.TbThiSinh_Truongs
                                                   join bb in db.TbTruongTHPTs on aa.IDTruong equals bb.IDTruong
                                                   where aa.Lop == 11 && aa.MaTS == a.MaTS
                                                   select bb.TenTruong).FirstOrDefault(),
                                    MaTinhTruong12 = (from aa in db.TbThiSinh_Truongs
                                                      join bb in db.TbTruongTHPTs on aa.IDTruong equals bb.IDTruong
                                                      join cc in db.TbTinhs on bb.MaTinh equals cc.MaTinh
                                                      where aa.Lop == 12 && aa.MaTS == a.MaTS
                                                      select cc.MaTinh).FirstOrDefault(),
                                    TenTinhTruong12 = (from aa in db.TbThiSinh_Truongs
                                                       join bb in db.TbTruongTHPTs on aa.IDTruong equals bb.IDTruong
                                                       join cc in db.TbTinhs on bb.MaTinh equals cc.MaTinh
                                                       where aa.Lop == 12 && aa.MaTS == a.MaTS
                                                       select cc.TenTinh).FirstOrDefault(),
                                    IDTruong12 = (from aa in db.TbThiSinh_Truongs
                                                  join bb in db.TbTruongTHPTs on aa.IDTruong equals bb.IDTruong
                                                  where aa.Lop == 12 && aa.MaTS == a.MaTS
                                                  select bb.IDTruong).FirstOrDefault(),
                                    TenTruong12 = (from aa in db.TbThiSinh_Truongs
                                                   join bb in db.TbTruongTHPTs on aa.IDTruong equals bb.IDTruong
                                                   where aa.Lop == 12 && aa.MaTS == a.MaTS
                                                   select bb.TenTruong).FirstOrDefault(),
                                    MaHuyen = a.MaHuyen,
                                    TenHuyen = f.TenHuyen,
                                    CMND = a.CMND,
                                    DiaChi = a.DiaChi,
                                    MaDoiTuong = a.MaDoiTuong,
                                    TenDoiTuong = g.TenDoiTuong,
                                    MaTinh = f.MaTinh,
                                    TenTinh = o.TenTinh,
                                    MaChuyenNganh = a.MaChuyenNganh,
                                    MaKhoiThi = l.MaKhoiThi,
                                    TenKhoiThi = l.TenKhoiThi,
                                    MaNganh = k.MaNganh,
                                    TenChuyenNganh = i.TenChuyenNganh,
                                    TenNganh = k.TenNganh,
                                    NgayDangKy = a.NgayDangKi,
                                    HinhThucDK = a.HinhThucDK,
                                    NgayDongTien = a.NgayDongTien,
                                    NgayHoanThanhDK = a.NgayHoanThanhDK,
                                    MaBacDaoTao = c.MaBacDT,
                                    TenBacDaoTao = d.TenBacDT + " - " + d.TbHeDaoTao.TenHeDaoTao,
                                }).Distinct().AsEnumerable();
           
            if (!String.IsNullOrEmpty(item.MaTS))
            {
                //tkts1 = tkts1.Where(m => m.MaTS == item.MaTS).ToList();
                CandidateList = CandidateList.Where(m => m.MaTS.Contains(item.MaTS));
            }
            if (!String.IsNullOrEmpty(item.HoDem))
            {
                CandidateList = CandidateList.Where(m => m.HoDem.Contains(item.HoDem));
            }
            if (!String.IsNullOrEmpty(item.Ten))
            {
                CandidateList = CandidateList.Where(m => m.Ten.Contains(item.Ten));
            }
            if (!String.IsNullOrEmpty(MaTinh) && MaTinh.Trim() != "0")
            {
                CandidateList = CandidateList.Where(m => m.MaTinh.Equals(MaTinh));
            }
            if (!String.IsNullOrEmpty(item.MaHuyen) && item.MaHuyen.Trim() != "0")
            {
                CandidateList = CandidateList.Where(m => m.MaHuyen == item.MaHuyen);
            }
            if (!String.IsNullOrEmpty(item.DiaChi))
            {
                CandidateList = CandidateList.Where(m => m.DiaChi.Contains(item.DiaChi));
            }
            if (!String.IsNullOrEmpty(item.CMND))
            {
                CandidateList = CandidateList.Where(m => m.CMND.Contains(item.CMND));
            }
            if (!String.IsNullOrEmpty(MaTinh2) && MaTinh2.Trim() != "0")
            {
                CandidateList = CandidateList.Where(m => m.MaTinhTruong10.Equals(MaTinh2) || m.MaTinhTruong11.Equals(MaTinh2) || m.MaTinhTruong12.Equals(MaTinh2));
            }
            if (!String.IsNullOrEmpty(IDTruong) && IDTruong.Trim() != "0")
            {
                int ma = int.Parse(IDTruong);
                CandidateList = CandidateList.Where(m => m.IDTruong10 == ma || m.IDTruong11 == ma || m.IDTruong12 == ma);
            }
            if (!String.IsNullOrEmpty(item.SBD))
            {
                CandidateList = CandidateList.Where(m => m.SBD.Contains(item.SBD));
            }
            if (!String.IsNullOrEmpty(item.SoDienThoai))
            {
                CandidateList = CandidateList.Where(m => m.SoDienThoai.Contains(item.SoDienThoai));
            }
            if (!String.IsNullOrEmpty(HDT) && HDT.Trim() != "0")
            {
                int mahdt = int.Parse(HDT);
                CandidateList = CandidateList.Where(m => m.MaHeDaoTao == mahdt);
            }
            if (!String.IsNullOrEmpty(BDT) && BDT.Trim() != "0")
            {
                int mabdt = int.Parse(BDT);
                CandidateList = CandidateList.Where(m => m.MaBacDaoTao == mabdt);
            }
            if (!String.IsNullOrEmpty(MaKhoiThi) && MaKhoiThi.Trim() != "0")
            {
                CandidateList = CandidateList.Where(m => m.MaKhoiThi.Trim() == MaKhoiThi.Trim());
            }
            if (!String.IsNullOrEmpty(MaNganh) && MaNganh.Trim() != "0")
            {
                //int manganh = int.Parse(MaNganh);
                CandidateList = CandidateList.Where(m => m.MaNganh == MaNganh);
            }
            if (!String.IsNullOrEmpty(item.MaChuyenNganh) && item.MaChuyenNganh.Trim() != "0")
            {
                CandidateList = CandidateList.Where(m => m.MaChuyenNganh == item.MaChuyenNganh);
            }
            if (DaNop && ChuaNop)
            {
                //CandidateList = CandidateList.Where(m => m.NgayDongTien != null);
            }
            else
            {
                if (ChuaNop)
                {
                    CandidateList = CandidateList.Where(m => m.NgayDongTien == null);
                }
                if (DaNop)
                {
                    CandidateList = CandidateList.Where(m => m.NgayDongTien != null);
                }
            }
            
            return PartialView("_ResultCandidate", CandidateList);
        }
        [HttpPost]
        public ActionResult ExportCandidate(string contentType, string base64, string fileName)
        {
            var fileContents = Convert.FromBase64String(base64);

            return File(fileContents, contentType, fileName);
        }
        public ActionResult SearchMark()
        {
            //Database.SetInitializer<TS2015Context>(null);
            // do du lieu len dropdownlist

            if (Request.IsAjaxRequest())
                return PartialView();
            return View();
        }
        [HttpPost]
        public ActionResult SearchMark(TbThiSinh item)
        {
            var CandidateList = from a in db.TbThiSinhs
                                join b in db.TbDanTocs on a.MaDanToc equals b.MaDanToc
                                //join c in db.TbTruongTHPTs on a.IDTruong equals c.IDTruong
                                //join d in db.TbTinhs on c.MaTinh equals d.MaTinh
                                //join e in db.TbKhuVucs on c.MaKhuVuc equals e.MaKhuVuc
                                join f in db.TbHuyens on a.MaHuyen equals f.MaHuyen
                                join o in db.TbTinhs on f.MaTinh equals o.MaTinh
                                join g in db.TbDoiTuongUuTiens on a.MaDoiTuong equals g.MaDoiTuong
                                join h in db.TbTruongDHs on a.MaTruongDH equals h.MaTruongDH
                                join i in db.TbChuyenNganhs on a.MaChuyenNganh equals i.MaChuyenNganh
                                join k in db.TbNganhs on i.IDNganh equals k.IDNganh
                                join l in db.TbKhoiThis on k.MaKhoiThi equals l.MaKhoiThi
                                //join m in db.TbThiSinh_HeDaoTaos on a.MaTS equals m.MaTS
                                //join y in db.TbHeDaoTaos on m.MaHeDaoTao equals y.MaHeDaoTao
                                join n in db.TbDiemTotNghieps on a.MaTS equals n.MaTS
                                join z in db.TbMonTotNghieps on n.MaMon equals z.MaMon

                                select new ResultTK
                                {
                                    MaMon = z.MaMon,
                                    TenMon = z.TenMon,
                                    MaTS = a.MaTS,
                                    HoDem = a.HoDem,
                                    Ten = a.Ten,
                                    FullName = a.HoDem + a.Ten,
                                    SBD = a.SBD,
                                    MaDanToc = a.MaDanToc,
                                    TenDanToc = b.TenDanToc,
                                    GioiTinh = a.GioiTinh,
                                    SoDienThoai = a.SoDienThoai,
                                    CMND = a.CMND,
                                    Email = a.Email,
                                    NgaySinh = a.NgaySinh,
                                    //IDTruong = a.IDTruong,
                                    //MaTruong = c.MaTruong,
                                    //TenTruong = c.TenTruong,
                                    MaHuyen = a.MaHuyen,
                                    TenHuyen = f.TenHuyen,
                                    DiaChi = a.DiaChi,
                                    DiemLop10 = a.DiemLop10,
                                    DiemLop11 = a.DiemLop11,
                                    DiemLop12 = a.DiemLop12,
                                    DiemTBTHPT = a.DiemTBTHPT,
                                    DiemTBTN = a.DiemTBTN,
                                    MaDoiTuong = a.MaDoiTuong,
                                    TenDoiTuong = g.TenDoiTuong,
                                    //MaTinhTruong = c.MaTinh,
                                    //TenTinhTruong = d.TenTinh,
                                    MaTinh = f.MaTinh,
                                    TenTinh = o.TenTinh,
                                    MaChuyenNganh = a.MaChuyenNganh,
                                    //MaHeDaoTao = m.MaHeDaoTao,
                                    MaKhoiThi = l.MaKhoiThi,
                                    TenKhoiThi = l.TenKhoiThi,
                                    MaNganh = k.MaNganh,
                                    TenChuyenNganh = i.TenChuyenNganh,
                                    TenNganh = k.TenNganh,
                                    //TenHeDaoTao = y.TenHeDaoTao,
                                    NgayDangKy = a.NgayDangKi,
                                    NgayDongTien = a.NgayDongTien,
                                    NgayHoanThanhDK = a.NgayHoanThanhDK,
                                    NamTotNghiep = a.NamTotNghiep
                                };
            //if (!String.IsNullOrEmpty(item.MaTS))
            //{
            //    //tkts1 = tkts1.Where(m => m.MaTS == item.MaTS).ToList();
            //    CandidateList = CandidateList.Where(m => m.MaTS.Contains(item.MaTS));
            //}
            //if (!String.IsNullOrEmpty(item.HoDem))
            //{
            //    CandidateList = CandidateList.Where(m => m.HoDem.Contains(item.HoDem));
            //}
            //if (!String.IsNullOrEmpty(item.Ten))
            //{
            //    CandidateList = CandidateList.Where(m => m.Ten.Contains(item.Ten));
            //}
            //if (!String.IsNullOrEmpty(MaTinh))
            //{
            //    CandidateList = CandidateList.Where(m => m.MaTinh.Equals(MaTinh));
            //}
            //if (!String.IsNullOrEmpty(item.MaHuyen))
            //{
            //    CandidateList = CandidateList.Where(m => m.MaHuyen == item.MaHuyen);
            //}
            //if (!String.IsNullOrEmpty(item.DiaChi))
            //{
            //    CandidateList = CandidateList.Where(m => m.DiaChi.Contains(item.DiaChi));
            //}
            //if (!String.IsNullOrEmpty(item.CMND))
            //{
            //    CandidateList = CandidateList.Where(m => m.CMND.Contains(item.CMND));
            //}
            //if (!String.IsNullOrEmpty(IDTruong))
            //{
            //    int ma = int.Parse(IDTruong);
            //    CandidateList = CandidateList.Where(m => m.IDTruong == ma);
            //}
            //if (!String.IsNullOrEmpty(item.SBD))
            //{
            //    CandidateList = CandidateList.Where(m => m.SBD.Contains(item.SBD));
            //}
            //if (!String.IsNullOrEmpty(item.SoDienThoai))
            //{
            //    CandidateList = CandidateList.Where(m => m.SoDienThoai.Contains(item.SoDienThoai));
            //}
            //if (!String.IsNullOrEmpty(HDT))
            //{
            //    int mahdt = int.Parse(HDT);
            //    CandidateList = CandidateList.Where(m => m.MaHeDaoTao == mahdt);
            //}
            //if (!String.IsNullOrEmpty(MaNganh))
            //{
            //    //int manganh = int.Parse(MaNganh);
            //    CandidateList = CandidateList.Where(m => m.MaNganh == MaNganh);
            //}
            //if (!String.IsNullOrEmpty(item.MaChuyenNganh))
            //{
            //    CandidateList = CandidateList.Where(m => m.MaChuyenNganh == item.MaChuyenNganh);
            //}
            //if (DaNop && ChuaNop)
            //{
            //    //CandidateList = CandidateList.Where(m => m.NgayDongTien != null);
            //}
            //else
            //{
            //    if (ChuaNop)
            //    {
            //        CandidateList = CandidateList.Where(m => m.NgayDongTien == null);
            //    }
            //    if (DaNop)
            //    {
            //        CandidateList = CandidateList.Where(m => m.NgayDongTien != null);
            //    }
            //}

            return PartialView("_ResultCandidate", CandidateList);
        }
        #endregion
        #region Search HighSchool
        public ActionResult SearchHighSchool()
        {
            //Database.SetInitializer<TS2015Context>(null);
            // do du lieu len dropdownlist
           
            ViewBag.KhuVuc = db.TbKhuVucs.ToList();
            ViewBag.Tinh = new SelectList(db.TbTinhs.ToList(), "MaTinh", "TenTinh");
            ViewBag.KhuVuc = new SelectList(db.TbKhuVucs.ToList(), "MaKhuVuc", "TenKhuVuc");
            
            if (Request.IsAjaxRequest())
                return PartialView();
            return View();
        }
        [HttpPost]
        [ActionName("SearchHighSchool")]
        public ActionResult POST_SearchHighShool(TbTruongTHPT item)
        {
            ViewBag.KhuVuc = db.TbKhuVucs.ToList();
            ViewBag.Tinh = db.TbTinhs.ToList();

            var HighSchoolList = from a in db.TbTruongTHPTs
                                 join b in db.TbTinhs on a.MaTinh equals b.MaTinh
                                 join c in db.TbKhuVucs on a.MaKhuVuc equals c.MaKhuVuc
                                 select new ResultHighSchool
                                 {
                                     IDTruong = a.IDTruong,
                                     MaTruong = a.MaTruong,
                                     TenTruong = a.TenTruong,
                                     MaTinh = a.MaTinh,
                                     DiaChi = a.DiaChi,
                                     TenTinh = b.TenTinh,
                                     MaKhuVuc = a.MaKhuVuc,
                                     TenKhuVuc = c.TenKhuVuc
                                 };
            
            if (!String.IsNullOrEmpty(item.MaTruong))
            {
                HighSchoolList = HighSchoolList.Where(m => m.MaTruong.Contains(item.MaTruong));
            }
            if (!String.IsNullOrEmpty(item.TenTruong))
            {
                HighSchoolList = HighSchoolList.Where(m => m.TenTruong.Contains(item.TenTruong));

            }
            if (!String.IsNullOrEmpty(item.MaTinh))
            {

                HighSchoolList = HighSchoolList.Where(m => m.MaTinh.Contains(item.MaTinh));
            }
            if (!String.IsNullOrEmpty(item.DiaChi))
            {
                HighSchoolList = HighSchoolList.Where(m => m.DiaChi.Contains(item.DiaChi));
            }
            if (!String.IsNullOrEmpty(item.MaKhuVuc))
            {
                HighSchoolList = HighSchoolList.Where(m => m.MaKhuVuc.Contains(item.MaKhuVuc));
            }

            return PartialView("_ResultHighSchool", HighSchoolList);
        }
        public ActionResult ExportHighSchool(string contentType, string base64, string fileName)
        {
            var fileContents = Convert.FromBase64String(base64);

            return File(fileContents, contentType, fileName);
        }
        #endregion
        #region Search University
        public ActionResult SearchUniversity()
        {
            //Database.SetInitializer<TS2015Context>(null);
            // do du lieu len dropdownlist
        
            if (Request.IsAjaxRequest())
                return PartialView();
            return View();
        }
        [HttpPost]
        [ActionName("SearchUniversity")]
        public ActionResult POST_SearchUniversity(TbTruongDH item)
        {

            var ListUniversitys = from c in db.TbTruongDHs select c;

            if (!String.IsNullOrEmpty(item.MaTruongDH))
            {
                ListUniversitys = ListUniversitys.Where(m => m.MaTruongDH.Contains(item.MaTruongDH));
            }
            if (!String.IsNullOrEmpty(item.TenTruongDH))
            {
                ListUniversitys = ListUniversitys.Where(m => m.TenTruongDH.Contains(item.TenTruongDH));

            }
            if (!String.IsNullOrEmpty(item.DiaChi))
            {
                ListUniversitys = ListUniversitys.Where(m => m.DiaChi.Contains(item.DiaChi));
            }

            return PartialView("_ResultUniversity", ListUniversitys);
        }
        public ActionResult ExportUniversity(string contentType, string base64, string fileName)
        {
            var fileContents = Convert.FromBase64String(base64);

            return File(fileContents, contentType, fileName);
        }
        #endregion

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}